import os

from PyQt5.QtWidgets import QMainWindow, QGridLayout, QMessageBox, QInputDialog, QApplication, QMenu, \
    QAction, QWidget

from config.config import db
from model.note.noteWidget import NoteWidget
from util.singleton import Singleton
from view.ui_mainWindow import Ui_MainWindow
from util.qt import qtUtil


@Singleton
class MainWindow(Ui_MainWindow, QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setupUi(self)

        self._presentWidget = None
        self.actionNote.triggered.connect(lambda: self.changeWidget(NoteWidget()))
        self.changeWidget(NoteWidget())

    def exit(self):
        if qtUtil.execMessageBox('确定要退出程序吗？') == QMessageBox.Ok:
            self.close()
            QApplication.exit(0)

    def changeWidget(self, widget):
        """
        切换当前窗口
        """
        if type(self._presentWidget) == type(widget):
            return
        if self._presentWidget is not None:
            self._presentWidget.close()
        self._presentWidget = widget
        qtUtil.nestWidget(self.centralwidget, widget)
        widget.show()
